package com.jack.helloworld.activity;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

import com.jack.helloworld.R;


public class ScrollViewActivity extends AppCompatActivity implements View.OnTouchListener, View.OnClickListener {

    ScrollView scrollView;
    TextView tvScrollView;

    Button scrollUp;
    Button scrollDown;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scroll_view);
        
        initView();
        initData();
    }

    private void initView() {
        scrollView = (ScrollView) findViewById(R.id.scrollView1);
        tvScrollView = (TextView) findViewById(R.id.tvScrollView);
        scrollUp = (Button) findViewById(R.id.scrollUp);
        scrollDown = (Button) findViewById(R.id.scrollDown);

    }

    private void initData() {
        scrollView.setOnTouchListener(this);
        scrollUp.setOnClickListener(this);
        scrollDown.setOnClickListener(this);
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_MOVE:
                if (scrollView.getScrollY() <= 0) {
                    Log.d("jack", "滑到顶部");
                } else if (scrollView.getChildAt(0).getMeasuredHeight() == scrollView.getHeight() + scrollView.getScrollY()) {
                    Log.d("jack", "滑到底部");
                    Log.d("jack", "view.getMeasuredHeight():"+ view.getMeasuredHeight()
                            + " ,view.getHeight():" + view.getHeight() + " ,view.getScrollY():" + view.getScrollY());
                    tvScrollView.append(tvScrollView.getText());
                }

                break;
        }

        return false;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.scrollUp:
//                scrollView.scrollTo(0, -30);
                scrollView.scrollBy(0, -30);
                break;
            case R.id.scrollDown:
//                scrollView.scrollTo(0, 30);
                scrollView.scrollBy(0, 30);
                break;
        }
    }
}
